package com.alien.admin.modules.publisher.service.impl;

import com.alien.admin.modules.publisher.domain.PublishSheetFlow;
import com.alien.admin.modules.publisher.service.dto.PublishSheetFlowDto;
import com.alien.admin.utils.EnvConstant;
import com.alien.admin.utils.enums.FlowNodeIdEnum;
import com.alien.admin.utils.enums.FlowRoleEnum;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Copyright (C), 2014-2021, 厦门神州鹰软件科技有限公司
 * FileName: PublishSheetFlowUtils
 *
 * @author: zengzf
 * Date:     2021/10/25 10:24
 * Description:
 * History:
 */
public class PublishSheetFlowUtils {

    /**
     * 根据flowNodeId获取这个节点的角色
     *
     * @param flowNodeId
     * @return
     */
    public static FlowRoleEnum getRoleByFlowNodeId(Integer flowNodeId) {
        FlowNodeIdEnum flowNodeIdEnum = FlowNodeIdEnum.getById(flowNodeId);
        if(flowNodeIdEnum == null) {
            return FlowRoleEnum.Nobody;
        }
        switch (flowNodeIdEnum) {
            case EnvRcDeveloper:
            case EnvProdDeveloper:
                return FlowRoleEnum.Developer;
            case SubmitByDeveloper:
                return FlowRoleEnum.Manager;
            case EnvTestTester:
            case EnvRcTester:
            case EnvProdTester:
                return FlowRoleEnum.Tester;
            case EnvRcDeployer:
            case EnvProdDeployer:
                return FlowRoleEnum.Deployer;
            case DraftByDeveloper:
            case Finish:
                return FlowRoleEnum.Nobody;
            default:
                // do nothing
        }
        return FlowRoleEnum.Nobody;
    }

    /**
     * 根据角色获得当前处理人id（processorId）
     *
     * @param role             当前处理点的对应角色
     * @param publishSheetFlow 单据
     * @return
     */
    public static Long getProcessorIdByRole(FlowRoleEnum role, PublishSheetFlow publishSheetFlow) {
        switch (role) {
            case Developer:
                return publishSheetFlow.getApplierId();
            case Manager:
                return publishSheetFlow.getManagerId();
            case Tester:
                return publishSheetFlow.getTesterId();
            case Deployer:
                return publishSheetFlow.getDeployerId();
            case Nobody:
                return null;
        }
        return null;
    }

    /**
     * 根据流程节点id，获得下一个处理者。
     *
     * @param publishSheetFlow
     * @return
     */
    public static Long getProcessorIdByPublishSheetFlow(PublishSheetFlow publishSheetFlow) {
        FlowRoleEnum flowRoleEnum = getRoleByFlowNodeId(publishSheetFlow.getFlowNodeId());
        if (flowRoleEnum == null) {
            return null;
        }
        return getProcessorIdByRole(flowRoleEnum, publishSheetFlow);
    }

    /**
     * 判断是否在测试人员节点上
     *
     * @return
     */
    public static Boolean isTesterNode(Integer flowNodeId) {
        FlowNodeIdEnum nodeIdEnum = FlowNodeIdEnum.getById(flowNodeId);
        if (nodeIdEnum == null) {
            return false;
        }
        if (nodeIdEnum.name().indexOf("Tester") > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取流程编号
     *
     * @return
     */
    public static String getPublishSheetNo() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String sheetNo = df.format(new Date());
        return "PB" + sheetNo;
    }

    public static String getEnvByFlowNode(Integer nodeId) {
        String env = EnvConstant.DEBUG;
        if(FlowNodeIdEnum.EnvTestTester.getId().equals(nodeId)) {
            env = EnvConstant.TEST;
        } else if(FlowNodeIdEnum.EnvRcDeployer.getId().equals(nodeId)) {
            env = EnvConstant.RC;
        } else if(FlowNodeIdEnum.EnvProdDeployer.getId().equals(nodeId)) {
            env = EnvConstant.PROD;
        }

        return env;
    }
}

